import math;
from numpy import *;
from scipy import *;
from random import *;
from copy import deepcopy;

ROUNDING = 2;


def cmp_wav_osc_lst(in_wav, in_osc, in_mask):
  '''Primerjava parametrov (osciliranje, amplituda, frekvenca, cas vzpona, cas padca)'''

  cmp_list = [float("inf") for i in range(3)];

  if  in_osc.is_osc() == 0:
    return cmp_list;
  
  #funkcije, ki jih minimiziramo
  cmp_amp         = math.fabs(in_osc.get_amp()-in_wav.get_amp())            / in_wav.get_amp();
  cmp_frq         = math.fabs(in_osc.get_frq()-in_wav.get_frq())            / in_wav.get_frq();
  cmp_wl          = math.fabs(in_osc.get_per()-in_wav.get_per())              / in_wav.get_per();
  cmp_sym         = math.fabs(in_osc.get_tim_up() - in_osc.get_tim_down())
  cmp_std         = std(in_osc.get_peaks());
  
  cmp_list        = []
  if in_mask[0] == 1:
    cmp_list        =cmp_list + [cmp_amp]
  if in_mask[1] == 1:
    cmp_list        =cmp_list + [cmp_frq]
  if in_mask[2] == 1:
    cmp_list        =cmp_list + [cmp_sym]

  return cmp_list

def cmp_wav_osc_fitness(in_wav, in_osc, in_mask):
  '''Fitness funkcija (neutezena)'''
  return mean(cmp_wav_osc_lst(in_wav, in_osc, in_mask));
 
def gen_osc_repr(in_osc1, p_mut, in_mmask):
  '''Reprodukcija in mutacije'''
  r3 = deepcopy(in_osc1.get_rapams());
  
  for i in range(len(r3)):
    sign = 1;
    if random() < p_mut and in_mmask[i]:
      if random() < 0.5:
        sign = -1;

      r3[i] = math.fabs(r3[i] + sign * random());

  return r3;


def det_rounding(in_amp):
    '''Dolocimo velikost zaokrozevanja. Vneseni natancnosti dodamo se 3 decimalke'''
    r = len(str(float(in_amp)).split('.')[1]) + 3;
    return r

  
  
   
